<template>
  <div>
    <van-nav-bar fixed>
      <template #left>
        <img :src="objimg" alt="" class="myimg" />
      </template>
      <template #right>
        <van-icon name="search" color="#fff" size="0.48rem" @click="$router.push('/search')"/>
      </template>
    </van-nav-bar>
    <!-- <van-tabs
    v-model="active"
    sticky
    animated
    offset-top="1.226667rem"
    >
  <van-tab v-for="item in ChannelLsit" :title="item.name" :key="item.id">
    内容 {{ item.name }}
  </van-tab>
</van-tabs> -->

    <!-- 频道部分 -->
    <van-tabs v-model="channelId" animated sticky offset-top="1.226667rem">
      <van-tab
        v-for="item in ChannelLsit"
        :key="item.id"
        :title="item.name"
        :name="item.id"
      >
        <ArticleList :cid="channelId"></ArticleList>
      </van-tab>
    </van-tabs>
    <van-icon
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
      @click="show = true"
    />
    <!-- 弹出层组件 -->
    <van-popup v-model="show" class="edit_wrap" @close="closePopup">
      <!-- 弹出层的主体区域 -->
      <channel-edit
        :ChannelLsit="ChannelLsit"
        @addbtn="addbtn"
        @removeFn="removeFn"
        @close="show = false"
        @changeChanne="changeChanne"
        ref="EditRef"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import ArticleList from '@/views/Home/ArticleList'
import ChannelEdit from '@/views/Home/ChannelEdit'
import objimg from '../../assets/toutiao_logo.png'
import { userChannelAPI, FgaiReportsAPI } from '@/api'
export default {
  data () {
    return {
      objimg,
      channelId: 0,
      ChannelLsit: [],
      show: false
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  async created () {
    const res = await userChannelAPI()
    this.ChannelLsit = res.data.data.channels
  },
  methods: {
    async qust () {
      const list = this.ChannelLsit.filter((item) => item.id !== 0)
      const arr = list.map((items, index) => {
        const newObj = { ...items }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      await FgaiReportsAPI({
        channels: arr
      })
    },
    async addbtn (item) {
      this.ChannelLsit.push(item)
      this.qust()
    },
    removeFn (item, indexs) {
      // this.ChannelLsit.splice(index, 1)
      const index = this.ChannelLsit.findIndex((items) => items.id === item.id)
      this.ChannelLsit.splice(index, 1)
      this.qust()
    },
    closePopup () {
      this.$refs.EditRef.isEeit = false
    },
    changeChanne (item) {
      this.channelId = item.id
    }
  }
}
</script>

<style lang="less" scoped>
.myimg {
  width: 100px;
  height: 30px;
}
/* tab内容距离tab导航的距离 */
/deep/ .van-tabs__content {
  padding-top: 44px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
/* 频道编辑弹出层铺满屏幕 */
.edit_wrap {
  width: 100%;
  height: 100%;
}
</style>
